//
//  MaintainViewController.m
//  Commerce
//
//  Created by xiaofeifei on 15/3/3.
//  Copyright (c) 2015年 fei. All rights reserved.
//

#import "MaintainViewController.h"

@interface MaintainViewController (){
    BMKMapView* _mapView;
    
    NSMutableArray *_annotationList;
}

@end

@implementation MaintainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navStr = @"周边维修";

    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, self.navView.frame.origin.y + self.navView.frame.size.height , 320, self.view.frame.size.height - self.navView.frame.size.height)];
    [self.view addSubview:_mapView];
   
    _annotationList = [[NSMutableArray alloc] init];
    [self getMaintainData];
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated
{
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
    _mapView.userTrackingMode = BMKUserTrackingModeFollow;
    _mapView.showsUserLocation = YES;//开启定位服务
//    BMKCoordinateRegion region = BMKCoordinateRegionMake([[GlobalData sharedGlobalData] uselocation], BMKCoordinateSpanMake(0.05, 0.05));
//    [_mapView setRegion:region animated:YES];
    [_mapView updateLocationData:[[GlobalData sharedGlobalData] bUserLocation]];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
}

#pragma mark -getdata
- (void)getMaintainData{
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    [parameters addEntriesFromDictionary:@{@"dong_jing":[NSString stringWithFormat:@"%f",[[GlobalData sharedGlobalData] uselocation].longitude],@"bei_wei":[NSString stringWithFormat:@"%f",[[GlobalData sharedGlobalData] uselocation].latitude]}];
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@per/list",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  id responseObject = result;
                                  _annotationList = responseObject;
                                  [self setAnnotionsWithList:responseObject];
                                  [SVProgressHUD dismiss];
                                  
                              }];
}

-(void)setAnnotionsWithList:(NSArray *)list
{
    for (NSDictionary *dic in list) {
        
        CLLocationDegrees latitude=[[dic objectForKey:@"BeiWei"] doubleValue];
        CLLocationDegrees longitude=[[dic objectForKey:@"DongJing"] doubleValue];
        
        BMKPointAnnotation *pointAnnotation = [[BMKPointAnnotation alloc]init];
        CLLocationCoordinate2D coor;
        coor.latitude = latitude;
        coor.longitude = longitude;
        pointAnnotation.coordinate = coor;
        pointAnnotation.title = dic[@"PerName"];
        pointAnnotation.subtitle = [NSString stringWithFormat:@"%@,%@,%@",dic[@"Address"],[dic[@"tel"] isEqual:[NSNull null]]?@"":dic[@"tel"],[dic[@"Character"] isEqual:[NSNull null]]?@"":dic[@"Character"]];
        [_mapView addAnnotation:pointAnnotation];
    }
}

#pragma mark -
#pragma mark implement BMKMapViewDelegate


// 根据anntation生成对应的View
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    //普通annotation
    NSString *AnnotationViewID = @"renameMark";
    BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
    if (annotationView == nil) {
        annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
        // 设置颜色
        annotationView.pinColor = BMKPinAnnotationColorPurple;
        // 从天上掉下效果
        annotationView.animatesDrop = YES;
        // 设置可拖拽
        annotationView.draggable = NO;
    }
    return annotationView;
}

// 当点击annotation view弹出的泡泡时，调用此接口
- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view;
{
    NSLog(@"paopaoclick");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
